package com.adv.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
	public static boolean copyFile(File from, File to) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(from));
			out = new BufferedOutputStream(new FileOutputStream(to));

			int len = 0;
			byte[] b = new byte[102400];

			while ((len = in.read(b)) != -1) {
				out.write(b, 0, len);
			}
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				in.close();
				in = null;
				out.close();
				out = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void delete(File directory) {
		if (directory.isFile()) {
			directory.delete();
			return;
		}

		File[] fileArr = directory.listFiles();
		for (File file : fileArr)
			delete(file);
	}
}